/**
 * Created by Mission on 16/10/10.
 */
$(function () {
//获取页面间的url的参数
    (function ($) {
        $.getUrlParam = function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        }
    })(jQuery);
    var index = $.getUrlParam('index');
    console.log("index:" + index);

    //刷新数据最小的起始id
    var min_id_page1;
    var min_id_page2;
    var min_id_page3;
    var stop = true;//触发开关，防止多次调用事件

    initData("content1", user_id, "0", "0");

    initData("content2", user_id, "0", "1");

    initData("content3", user_id, "0", "2");

    //获取
//数据展示方法...
    /*
     * id         数据展示的div的id
     * user_id    当前行的用户id
     * user_vote_id   列表类型
     * status     状态("全部","我发起的","我参与的")
     *
     * */
    function initData(id, user_id, user_vote_id, status) {
        var html;
        var url;
        var data = {
            status: status,
            user_id: user_id,
            group_id: group_id,
            user_vote_id: user_vote_id,
            token: token,
        };
        // var apiurl = "http://192.168.3.100:8080/WorkAssistant/api/v1";
        url = apiurl + "/vote/list";
        console.log(data);
        var status_colors = [
            "#C5D556", "#61D064", "#B2B2B2", "#FFC933"];
        var status_text = [
            "草 稿", "进行中", "已结束", "已投票"];
        $.ajax({
            beforeSend: function () {
                $(".loading").show();
            },
            url: url,
            type: "GET",
            data: data,
            success: function (d) {
                $(".loading").hide();
                if (d.code == 1) {
                    if (d.msg == "202") {
                        //-----------------------------------------------
                        $('.token_iframe').show();
                    }
                } else {
                    console.log((parseInt(status) + 1) + ":d:" + d);

                    var userinfos = JSON.parse(d).data.userInfos;
                    var data = JSON.parse(d).data.voteVos;
                    console.log(userinfos);
                    if (data == "" || data == null || data == {}) {
                        //如果这是首次请求 没有数据 显示none层
                        if (user_vote_id == 0) {
                            $("#none" + (parseInt(status) + 1)).show();
                        }
                    } else if (data.length > 0) {
                        console.log(data[data.length - 1].vote_id);
                        $("#min_id" + status).val(data[data.length - 1].vote_id);
                        $("#none" + (parseInt(status) + 1)).hide();

                        for (var i = 0; i < data.length; i++) {
                            //初始化时间
                            var date = new Date(data[i].end_time);
                            initDate(date);
                            var userid = data[i].user_id;
                            var user_name;
                            if (data[i].is_view == 1) {
                                user_name = "匿名";
                            } else {
                                // 获取列表内子项的发起人名称：usrename
                                if (userinfos.length > 0) {
                                    for (var j = 0; j < userinfos.length; j++) {
                                        if (userinfos[j].id == userid) {
                                            user_name = userinfos[j].name;
                                        }
                                    }
                                }
                            }
                            if (data[i].status == 0) {
                                //草稿，不显示投票人数
                                html = "<div class='page-item' id='" +
                                    data[i].vote_id + "'><div class='content_left'><img src='../img/item-icon.png' class='left-img'/></div><div class='content_right'><h1 class='item-name'>" +
                                    data[i].vote_title + "</h1><span class='left'>" + user_name + "</span><div class='right'><div class='item_status'style='background: " + status_colors[data[i].status] + "'>" +
                                    status_text[data[i].status] + "</div></div><div class='time'><span>截止时间:</span><span>" +
                                    whole_time_horiz + "</span></div><div class='numb'></div></div></div>";
                            } else {
                                html = "<div class='page-item' id='" +
                                    data[i].vote_id + "'><div class='content_left'><img src='../img/item-icon.png' class='left-img'/></div><div class='content_right'><h1 class='item-name'>" +
                                    data[i].vote_title + "</h1><span class='left'>" + user_name + "</span><div class='right'><div class='item_status'style='background: " + status_colors[data[i].status] + "'>" +
                                    status_text[data[i].status] + "</div></div><div class='time'><span>截止时间:</span><span>" +
                                    whole_time_horiz + "</span></div><div class='numb'><span class='num'>" +
                                    data[i].nums + "</span><span>人投票</span></div></div></div>";
                            }

                            $("#" + id + "").append(html);

                        }

                    } else if (data.length < 10) {
                        //新加载的少于10条 脱绑
                        $(window).unbind('scroll');
                    }
                }
            },
            error: function (d) {
            },

        });

    }

    if (index == 1) {//我发起的

        $('#title-name').text("我发起的");
        $('#tabName2').css("color", "#C5D556");
        $('#tabImg2').attr("src", "../img/tab2on.png");
        $('#tabName1').css("color", "#929292");
        $('#tabImg1').attr("src", "../img/tab1off.png");
        $('#tabName3').css("color", "#929292");
        $('#tabImg3').attr("src", "../img/tab3off.png");
        // if ($('#page2').is(":hidden")) {
        $('#page2').show();
        $('#page1').hide();
        $('#page3').hide();
        stop = true;
    } else if (index == 2) {//我参与的
        $('#title-name').text("我参与的");
        $('#tabName3').css("color", "#C5D556");
        $('#tabImg3').attr("src", "../img/tab3on.png");
        $('#tabName2').css("color", "#929292");
        $('#tabImg2').attr("src", "../img/tab2off.png");
        $('#tabName1').css("color", "#929292");
        $('#tabImg1').attr("src", "../img/tab1off.png");
        // if ($('#page3').is(":hidden")) {
        $('#page3').show();
        $('#page1').hide();
        $('#page2').hide();
        stop = true;
    } else {
        $('#page1').show();
        $('#page2').hide();
        $('#page3').hide();
    }

    //跳转到创建投票按钮
    $(".add").click(function () {
        location.href = "creatVote.html";
    });
    //点击列表内子项，进行分开处理
    $(document).on("click", ".page-item", function () {
        var vote_id = $(this).attr("id");
        var status_txt = $(this).find('.right').text();
        if (status_txt == "草 稿") {
            //草稿跳转修改
            location.href = "creatVote.html?vote_id=" + vote_id + "";
        } else {
            location.href = "voteDetail.html?vote_id=" + vote_id + "" + "&index=" + index;
        }
    });
    //各个底部类别标签的点击事件
    $("#tab1").click(function () {
        index = 0;
        $('#title-name').text("投票列表");
        $('#tabName1').css("color", "#C5D556");
        $('#tabImg1').attr("src", "../img/tab1on.png");
        $('#tabName2').css("color", "#929292");
        $('#tabImg2').attr("src", "../img/tab2off.png");
        $('#tabName3').css("color", "#929292");
        $('#tabImg3').attr("src", "../img/tab3off.png");
        // if ($('#page1').is(":hidden")) {
        $('#page1').show();
        $('#page2').hide();
        $('#page3').hide();
        stop = true;
    });
    $("#tab2").click(function () {
        index = 1;
        $('#title-name').text("我发起的");
        $('#tabName2').css("color", "#C5D556");
        $('#tabImg2').attr("src", "../img/tab2on.png");
        $('#tabName1').css("color", "#929292");
        $('#tabImg1').attr("src", "../img/tab1off.png");
        $('#tabName3').css("color", "#929292");
        $('#tabImg3').attr("src", "../img/tab3off.png");
        // if ($('#page2').is(":hidden")) {
        $('#page2').show();
        $('#page1').hide();
        $('#page3').hide();
        stop = true;
    });
    $("#tab3").click(function () {
        index = 2;
        $('#title-name').text("我参与的");
        $('#tabName3').css("color", "#C5D556");
        $('#tabImg3').attr("src", "../img/tab3on.png");
        $('#tabName2').css("color", "#929292");
        $('#tabImg2').attr("src", "../img/tab2off.png");
        $('#tabName1').css("color", "#929292");
        $('#tabImg1').attr("src", "../img/tab1off.png");
        // if ($('#page3').is(":hidden")) {
        $('#page3').show();
        $('#page1').hide();
        $('#page2').hide();
        stop = true;
    });

    //下拉刷新
    $(window).scroll(function () {
        //当内容滚动到底部时加载新的内容 100当距离最底部100个像素时开始加载.
        if ($(this).scrollTop() + $(document).height() + 100 >= $(document).height()) {

            if (stop == true) {
                stop = false;
                if ($('#page1').css("display") != "none") {
                    min_id_page1 = $("#min_id0").val();
                    console.log("get_more:page1_" + min_id_page1);
                    if (min_id_page1 != null && min_id_page1 != "") {
                        initData("content1", user_id, min_id_page1, "0");
                    }
                }
                else if ($('#page2').css("display") != "none") {
                    min_id_page2 = $("#min_id1").val();
                    console.log("get_more:page2_" + min_id_page2);
                    if (min_id_page2 != null && min_id_page2 != "") {
                        initData("content1", user_id, min_id_page2, "1");
                    }
                }
                else if ($('#page3').css("display") != "none") {
                    min_id_page3 = $("#min_id2").val();
                    console.log("get_more:page3_" + min_id_page3);
                    if (min_id_page3 != null && min_id_page3 != "") {
                        initData("content1", user_id, min_id_page3, "2");
                    }

                }

            }
        }
    });
});